package com.oocl.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.tomcat.util.codec.binary.Base64;

public class Base64Util {

	public static String wrapBase64forjsp(byte[] bData){
		return "data:image/png;base64," + Base64.encodeBase64String(bData);
	}
	
	public static String wrapBase64Forsavepic(byte[] bData){
		return Base64.encodeBase64String(bData);
	}
	
	public static byte[] wrapBase64(String base64Str){
		return Base64.decodeBase64(base64Str);
	}
	
	public static String saveBase64Pic(String path, String base64Str){
		BufferedOutputStream out = null;
		ByteArrayInputStream in = null;
		try {
			File file = new File(path);
			out = new BufferedOutputStream(new FileOutputStream(file));
			byte[] picbytes = wrapBase64(base64Str);
			in = new ByteArrayInputStream(picbytes);
			
			byte[] buf = new byte[2048];
			while(in.read(buf)!=-1){
				out.write(buf);
			}
		} catch (FileNotFoundException e) {
			System.out.println("file not found..");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return path;
	}
	
}
